package org.xxpay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xxpay.core.entity.SysAreaCode;
import org.xxpay.core.entity.SysAreaCodeExample;
import org.xxpay.core.service.ISysAreaCodeService;
import org.xxpay.service.dao.mapper.SysAreaCodeMapper;

import java.util.List;


@Service
public class SysAreaCodeServiceImpl implements ISysAreaCodeService {

    @Autowired
    SysAreaCodeMapper sysAreaCodeMapper;

    @Override
    public int add(SysAreaCode sysAreaCode) {
        return sysAreaCodeMapper.insertSelective(sysAreaCode);
    }

    @Override
    public int update(SysAreaCode sysAreaCode) {
        return sysAreaCodeMapper.updateByPrimaryKeySelective(sysAreaCode);
    }

    @Override
    public SysAreaCode find(Integer areaCode) {
        return sysAreaCodeMapper.selectByPrimaryKey(areaCode);
    }

    @Override
    public List<SysAreaCode> select(SysAreaCode sysAreaCode) {
        SysAreaCodeExample example = new SysAreaCodeExample();
        SysAreaCodeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, sysAreaCode);
        return sysAreaCodeMapper.selectByExample(example);
    }

    void setCriteria(SysAreaCodeExample.Criteria criteria, SysAreaCode sysAreaCode) {
        if(sysAreaCode != null) {
            if(sysAreaCode.getAreaCode() != null) criteria.andAreaCodeEqualTo(sysAreaCode.getAreaCode());
            if(sysAreaCode.getCityCode() != null) criteria.andCityCodeEqualTo(sysAreaCode.getCityCode());
            if(sysAreaCode.getProvinceCode() != null) criteria.andProvinceCodeEqualTo(sysAreaCode.getProvinceCode());
        }
    }
}
